package gm.server.virtual.simulator2D;

import gm.editor.DoubleJSlider;
import gm.editor.JpegFileFilter;
import gm.editor.PngFileFilter;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Kanava kép panel.
 *
 * @author imruf84
 */
public class KanavaImagePanel extends javax.swing.JPanel {

    /**
     * Kanava kép.
     */
    private final KanavaImage kanavaImage;

    /**
     * Konstruktor.
     *
     * @param kanavaImage kép
     * @param panel panel
     */
    public KanavaImagePanel(final KanavaImage kanavaImage, final ScenePanel panel) {

        this.kanavaImage = kanavaImage;

        initComponents();

        ((DoubleJSlider) scaleSlider).setDoubleMinimum(0.01);
        ((DoubleJSlider) scaleSlider).setDoubleMaximum(5.0);

        scaleSlider.addChangeListener((ChangeEvent ce) -> {
            scaleValueLabel.setText(new DecimalFormat("#0.00").format(((DoubleJSlider) scaleSlider).getDoubleValue()));
            kanavaImage.setScale(((DoubleJSlider) scaleSlider).getDoubleValue());
            kanavaImage.updateTransform();
            panel.updateTipPanel();
        });

        updatePanel();
    }

    /**
     * Csúszka lekérdezése.
     *
     * @return csúszka
     */
    public JSlider getScaleSlider() {
        return scaleSlider;
    }

    /**
     * Felület frissítése.
     */
    public final void updatePanel() {
        ((DoubleJSlider) scaleSlider).setDoubleValue(kanavaImage.getScale());
        if (kanavaImage.getPath().equals("")) {
            fileNameValueLabel.setText("-");
        } else {
            fileNameValueLabel.setText(new File(kanavaImage.getPath()).getName());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileNameLabel = new javax.swing.JLabel();
        fileNameValueLabel = new javax.swing.JLabel();
        openImageButton = new javax.swing.JButton();
        scaleLabel = new javax.swing.JLabel();
        scaleSlider = new DoubleJSlider();
        scaleValueLabel = new javax.swing.JLabel();

        fileNameLabel.setText("Kép:");

        fileNameValueLabel.setText("-");

        openImageButton.setText("Megnyitás...");
        openImageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openImageButtonActionPerformed(evt);
            }
        });

        scaleLabel.setText("Méret:");

        scaleValueLabel.setText("-");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scaleSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(scaleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scaleValueLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(fileNameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fileNameValueLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(openImageButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fileNameLabel)
                    .addComponent(fileNameValueLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(openImageButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(scaleLabel)
                    .addComponent(scaleValueLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scaleSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void openImageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openImageButtonActionPerformed
        JFileChooser fc = new JFileChooser();
        fc.addChoosableFileFilter(new PngFileFilter());
        fc.setFileFilter(new JpegFileFilter());
        fc.setAcceptAllFileFilterUsed(false);

        int m = fc.showOpenDialog(this);
        if (m == JFileChooser.APPROVE_OPTION) {
            String fileName = fc.getSelectedFile().getAbsolutePath();
            try {
                kanavaImage.setImage(fileName);
                updatePanel();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(
                        this,
                        "Hiba a kép megnyitása során: " + ex.getLocalizedMessage(),
                        "Hiba",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_openImageButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel fileNameLabel;
    private javax.swing.JLabel fileNameValueLabel;
    private javax.swing.JButton openImageButton;
    private javax.swing.JLabel scaleLabel;
    private javax.swing.JSlider scaleSlider;
    private javax.swing.JLabel scaleValueLabel;
    // End of variables declaration//GEN-END:variables
}
